package com.hello.booksale.controller;

import com.hello.booksale.entity.AdminEntity;
import com.hello.booksale.service.QiniuService;
import com.hello.booksale.util.JwtUtil;
import com.hello.booksale.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Controller
public class IndexController {
    @Resource
    private JwtUtil jwtUtil;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private AdminEntity admin;
    @Resource
    private QiniuService qiniuService;

    @GetMapping("/")
    public String index() {
        return "redirect:/login";
    }

    @GetMapping("/login")
    public String login() {
        return "/login";
    }

    @GetMapping("/register")
    public String register() {
        return "/register";
    }

    @ResponseBody
    @PostMapping("/register")
    public String register(@RequestParam("mail")String mail,@RequestParam("password")String password,@RequestParam("code")int code){
        int num=redisUtil.getKeyValueFromRedis(mail);
        if(num==code)
        {
            return "ok";
        }
       return "error";
    }
    @ResponseBody
    @GetMapping("/code")
    public Map<String, Integer> code(@RequestParam("mail") String mail) {
        int code = (int) ((Math.random() * 9 + 1) * 100000);
        log.info("mail={},code={}", mail, code);
        redisUtil.writeKeyValueToRedis(mail, code);
        Map<String, Integer> map = new HashMap<>();
        map.put("code", code);
        return map;
    }
    @ResponseBody
    @GetMapping("/success")
    public String success() {
        return "success";
    }

    @GetMapping("/detail")
    public String detail() {
        return "book-detail";
    }

//    @PostMapping("/login")
//    public String login(HttpServletRequest request, HttpServletResponse response) {
//        String gRecaptchaResponse = request.getParameter("g-recaptcha-response");
//        String username = request.getParameter("username");
//        String token = jwtUtil.createToken(username);
//        Cookie cookie = new Cookie("token", token);
//        response.addCookie(cookie);
//        Verify.verify(gRecaptchaResponse);
//        return "redirect:/user/main";
//    }


}
